package org.jeecg.modules.company.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.wlhy.AmapClient;
import org.jeecg.modules.company.entity.WlhyCompany;
import org.jeecg.modules.company.entity.WlhyCompanyBothSides;
import org.jeecg.modules.company.mapper.WlhyCompanyBothSidesMapper;
import org.jeecg.modules.company.service.IWlhyCompanyBothSidesService;
import org.jeecg.modules.company.service.IWlhyCompanyService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static org.jeecg.common.constant.WlhyBizConstant.*;

/**
 * @Description: 公司常用发货双方表
 * @Author: jeecg-boot
 * @Date: 2024-04-12
 * @Version: V1.0
 */
@Service
@Slf4j
public class WlhyCompanyBothSidesServiceImpl extends ServiceImpl<WlhyCompanyBothSidesMapper, WlhyCompanyBothSides> implements IWlhyCompanyBothSidesService {


    @Resource
    AmapClient amapClient;
    @Resource
    IWlhyCompanyService wlhyCompanyService;

    @Override
    public void add(WlhyCompanyBothSides wlhyCompanyBothSides) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 检测企业状态
        WlhyCompany wlhyCompany = wlhyCompanyService.info();
        if (null == wlhyCompany || !COMPANY_AUTH_STATUS2.equals(wlhyCompany.getAuthStatus())) {
            throw new RuntimeException("当前企业未认证,请认证后再次操作");
        }
        if (StringUtils.isBlank(wlhyCompanyBothSides.getLnglat())) {
            // 经纬度解析
            try {
                JSONArray consignerAddrArray = amapClient.geocodeGeo(wlhyCompanyBothSides.getAreaName() + wlhyCompanyBothSides.getDetailAddr(), wlhyCompanyBothSides.getAreaCode());
                log.info("解析 经纬度 {}", consignerAddrArray);
                if (consignerAddrArray.size() == 0) {
                    throw new RuntimeException("未解析到地理位置,请写详细一点重新提交表单");
                }
                JSONObject consignerAddrObj = JSONObject.parseObject(JSON.toJSONString(consignerAddrArray.get(0)));
                wlhyCompanyBothSides.setLnglat(consignerAddrObj.getString("location"));
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage());
            }
        }
        wlhyCompanyBothSides.setUserId(loginUser.getId());
        wlhyCompanyBothSides.setStatus(ROW_STATUS1);
        wlhyCompanyBothSides.setCompanyId(wlhyCompany.getId());
        WlhyCompanyBothSides dbCompanyBothSides = getByNameAndIdNumAndCompanyIdAndSidesType(wlhyCompanyBothSides.getName(), wlhyCompanyBothSides.getIdNum(), wlhyCompanyBothSides.getCompanyId(), wlhyCompanyBothSides.getSidesType());
        if (null == dbCompanyBothSides) {
            wlhyCompanyBothSides.setId(null);
            save(wlhyCompanyBothSides);
        } else {
            wlhyCompanyBothSides.setId(dbCompanyBothSides.getId());
            updateById(wlhyCompanyBothSides);
        }

    }

    @Override
    public void updateStatusById(String id, String status) {
        update(new LambdaUpdateWrapper<WlhyCompanyBothSides>().eq(WlhyCompanyBothSides::getId, id).set(WlhyCompanyBothSides::getStatus, status));
    }

    @Override
    public void delete(String id) {
        updateStatusById(id, ROW_STATUS0);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        update(new LambdaUpdateWrapper<WlhyCompanyBothSides>().in(WlhyCompanyBothSides::getId, ids).set(WlhyCompanyBothSides::getStatus, ROW_STATUS0));

    }

    private WlhyCompanyBothSides getByNameAndIdNumAndCompanyIdAndSidesType(String name, String idNum, String companyId, String sidesType) {
        return getOne(new LambdaQueryWrapper<WlhyCompanyBothSides>()
                .eq(WlhyCompanyBothSides::getName, name)
                .eq(WlhyCompanyBothSides::getIdNum, idNum)
                .eq(WlhyCompanyBothSides::getCompanyId, companyId)
                .eq(WlhyCompanyBothSides::getSidesType, sidesType)
        );
    }


}
